library(flexdashboard)
###
library(ggplot2)
library(plotly)
library(plyr)
library(sf)
myWeb1="https://raw.githubusercontent.com/tianyl27/543_coursework_1/main/"
LifeExp_bar=readRDS(file=url(paste0(myWeb1,"plot1.rds")))
LifeExp2021 <- read.csv(file=url(paste0(myWeb1,'table1.csv')))
WHRData2021 <- read.csv(file=url(paste0(myWeb1,'WHR2021.csv')))
ggplotly(LifeExp_bar)
Error in FUN(X[[i]], ...) : object 'avg_LifeExp' not found
# title and caption text
TitleText = "Average Healthy Life Expectancy"
CaptionText = "Source: https://worldhappiness.report/ed/2021/"
# draw a picture of average life expectancy for each region
base3 = ggplot(data=LifeExp2021, aes(x=Region, y=LifeExp))+ theme_classic() + xlab("Region") + ylab("Healthy Life Expectancy") + scale_x_discrete(limits=LifeExp2021$Region)
# Get the average life expectancy for the whole world
avg_LifeExp = mean(WHRData2021$Healthy.life.expectancy)
low_regions=LifeExp2021[LifeExp2021$LifeExp<avg_LifeExp,"Region"]
low_regions
[1] "Commonwealth of Independent States" "Southeast Asia"
[3] "South Asia" "Sub-Saharan Africa"
annotation1=paste0('World Average:',round(avg_LifeExp,2))
LifeExpMin = min(LifeExp2021$LifeExp)
LifeExpMax = max(LifeExp2021$LifeExp)
annotation2=paste0('Min:',round(LifeExpMin,2))
annotation3=paste0('Max:',round(LifeExpMax,2))
bar3 = base3 + geom_bar(stat="identity", position = "dodge", aes(fill=LifeExp<avg_LifeExp), show.legend = F)+scale_fill_manual(values=c("grey","coral")) + ylim(0,80)
# add title and caption
bar4 = bar3 + coord_flip() + labs(title=TitleText, caption=CaptionText) + theme(plot.title = element_text(hjust = 0), plot.caption = element_text(hjust = -1))+ theme(axis.text.y = element_text(face=ifelse(LifeExp2021$Region%in%low_regions,"bold","plain")))
Warning: Vectorized input to `element_text()` is not officially supported.
Results may be unexpected or may change in future versions of ggplot2.
# add annotations
bar6 = bar4 + geom_hline(yintercept = round(avg_LifeExp,2), linetype=3, size=1, alpha=0.8)+ annotate(geom = 'text',label=annotation1,y = avg_LifeExp,x=5.5,angle=0) + annotate(geom = 'text',label=annotation2,y = LifeExpMin,x=10,angle=0) + annotate(geom = 'text',label=annotation3,y = LifeExpMax,x=1,angle=0)+theme(legend.position='none')
bar6
#ANZ: Australia and New Zealand
myWeb2="https://raw.githubusercontent.com/tianyl27/543_coursework_2/main/"
CovidDeath=readRDS(file=url(paste0(myWeb2,"CovidDeath.rds")))
ggplotly(CovidDeath)
No summary function supplied, defaulting to `mean_se()`
No summary function supplied, defaulting to `mean_se()`
GiniAge=readRDS(file=url(paste0(myWeb2,"GiniAndAge.rds")))
# ggplotly(GiniAge)
myWeb3="https://raw.githubusercontent.com/tianyl27/543_coursework_3/main/"
CountryMap=readRDS(file=url(paste0(myWeb3,"CountryMap.rds")))
ggplotly(CountryMap)
linkMap="https://github.com/EvansDataScience/VAforPM_Spatial/raw/main/worldMap.geojson"
library(sf)
mapWorld=read_sf(linkMap)
base=ggplot(data=mapWorld) + geom_sf(fill='grey90',
color=NA) + theme_classic()
WHRData2021
Map2021=readRDS(file=url(paste0(myWeb3,"mapWorldVars.rds")))
# leaflet
TitleText = "Country Performance in COVID epidemic"
CaptionText = "Source: https://worldhappiness.report/ed/2021/"
theLegTitle="World_Performance\n(grey is missing)"
clusterMap= base + geom_sf(data=Map2021,
aes(fill=cluster,text=NAME),
color=NA)
Warning: Ignoring unknown aesthetics: text
clusterMap = clusterMap+ scale_fill_brewer(palette ='BuGn',
direction = -1,
name=theLegTitle)+ labs(title=TitleText,
caption=CaptionText) +
theme(plot.title = element_text(hjust = 1),
plot.caption = element_text(hjust = 0),
axis.title.x = element_blank())
# palette ='BuGn'
# Next: change the color
# How to get rid of the axis
clusterMap%>%ggplotly()
factpal <- colorFactor(topo.colors(5), Map2021$cluster)
leaflet(Map2021) %>%addTiles() %>%
addPolygons(fillColor = ~factpal(cluster),
weight = 1,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7,
label = ~paste(NAME,": ", cluster),
highlightOptions = highlightOptions(
weight = 3,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE)
) %>%
addLegend("bottomright", pal = factpal, values = ~cluster,
title = "Perfomance\n(grey is missing)",
labFormat = labelFormat(prefix = ""),
opacity = 1
)
NA
NA
NA